//
//  DDropTimeSlider.swift
//  Drop
//
//  Created by hushaohua on 2018/9/3.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit
import CoreGraphics
import HKnot

class DDropTimeSlider: UIView, UIGestureRecognizerDelegate {
    
    var beginTime:Date?{
        didSet{
            self.timeModel.beginTime = beginTime;
            self.layoutFixedIf();
            self.configBeginLayers();
            self.configDuration();
            self.configMask();
        }
    }
    
    func configBeginLayers(){
        if (self.timeModel.beginDayString != nil){
            self.beginDayLayer?.string = self.timeModel.beginDayString!;
        }
        if (self.timeModel.beginTimeString != nil){
            self.beginTimeLayer?.string = self.timeModel.beginTimeString!;
        }
    }
    func configEndLayers(){
        if (self.timeModel.endDayString != nil){
            self.endDayLayer?.string = self.timeModel.endDayString!;
        }
        if (self.timeModel.endTimeString != nil){
            self.endTimeLayer?.string = self.timeModel.endTimeString!;
        }
    }
    
    var endTime:Date?{
        didSet{
            self.timeModel.endTime = endTime;
            self.layoutFixedIf();
            self.configEndLayers();
            self.configDuration();
            self.configMask();
        }
    }
    func configMask() {
        if (self.beginTime != nil && self.endTime != nil){
            self.maskLayer.setDates(self.beginTime!, end: self.endTime!);
            self.maskLayer.setNeedsDisplay();
        }
    }
    func configDuration() {
        if (self.timeModel.durationString != nil){
            self.durationLayer?.stringSize = self.timeModel.durationSize;
            self.durationLayer?.string = self.timeModel.durationString! as NSString;
            self.durationLayer?.setNeedsDisplay();
        }
    }
    
    //MARK:- layout fixed layers
    func layoutFixedIf() {
        if (self.beginTime != nil && self.endTime != nil){
            self.layoutFixedLayoutLayers();
        }
    }
    
    func layoutFixedLayoutLayers() {
        var beginDayFrame = self.beginDayLayer!.frame;
        var beginTimeFrame = self.beginTimeLayer!.frame;
        var endDayFrame = self.endDayLayer!.frame;
        var endTimeFrame = self.endTimeLayer!.frame;
        var trackFrame = self.trackLayer.frame;
        var durationFrame = self.durationLayer!.frame;
        
        let maxLeftWidth = max(self.timeModel.beginDaySize.width, self.timeModel.beginTimeSize.width);
        
        beginDayFrame.origin.y = (30 - self.timeModel.beginDaySize.height) / 2;
        beginDayFrame.size.width = maxLeftWidth;
        self.beginDayLayer?.frame = beginDayFrame;
        
        beginTimeFrame.origin.y = 30 + (30 - self.timeModel.beginTimeSize.height) / 2;
        beginTimeFrame.size.width = maxLeftWidth;
        beginTimeFrame.size.height = self.timeModel.beginTimeSize.height;
        self.beginTimeLayer?.frame = beginTimeFrame;
        
        let maxRightWidth = max(self.timeModel.endDaySize.width, self.timeModel.endTimeSize.width);
        
        endDayFrame.origin.y = (30 - self.timeModel.endDaySize.height) / 2;
        endDayFrame.size.width = maxRightWidth;
        endDayFrame.origin.x = self.bounds.size.width - maxRightWidth;
        self.endDayLayer?.frame = endDayFrame;
        
        endTimeFrame.origin.y = 30 + (30 - self.timeModel.endTimeSize.height) / 2;
        endTimeFrame.size.width = maxRightWidth;
        endTimeFrame.origin.x = self.bounds.size.width - maxRightWidth;
        endTimeFrame.size.height = self.timeModel.endTimeSize.height;
        self.endTimeLayer?.frame = endTimeFrame;
        
        trackFrame.origin.x = maxLeftWidth;
        trackFrame.size.width = self.bounds.size.width - maxRightWidth - maxLeftWidth;
        trackFrame.origin.y = 30;
        trackFrame.size.height = 30;
        self.trackLayer.frame = trackFrame;
        
        self.maskLayer.frame = trackFrame;
        
        durationFrame.origin.x = trackFrame.origin.x;
        durationFrame.size.width = trackFrame.size.width;
        durationFrame.origin.y = 30 + 30 + (20 - self.timeModel.durationSize!.height) / 2;
        durationFrame.size.height = self.timeModel.durationSize!.height;
        self.durationLayer?.frame = durationFrame;
        
        trackFrame.origin.y = trackFrame.origin.y + 10;
        trackFrame.size.height = trackFrame.size.height - 20;
        trackFrame.size.width = 0;
        self.progressLayer.frame = trackFrame;
        
        var thumbFrame = self.thumbLayer.frame;
        thumbFrame.origin.x = trackFrame.origin.x - thumbFrame.size.width / 2;
        thumbFrame.origin.y = trackFrame.origin.y + (trackFrame.size.height - thumbFrame.size.height) / 2;
        self.thumbLayer.frame = thumbFrame;
    }
    
    //MARK:- time model
    //not public
    lazy var timeModel:DDropTimeModel = {
        var model = DDropTimeModel();
        model.timeFont = UIFont.systemFont(ofSize: 13);
        model.dayFont = UIFont.systemFont(ofSize: 11);
        return model;
    }();
    
    //MARK:- init
    override init(frame: CGRect) {
        super.init(frame: frame);
        self.layer.addSublayer(self.trackLayer);
        
        self.layer.addSublayer(self.progressLayer);
        self.addTextLayers();
        self.layer.addSublayer(self.thumbLayer);
        self.layer.addSublayer(self.maskLayer);
        
        self.addGestures();
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- layers
    var beginDayLayer:CATextLayer?;
    var beginTimeLayer:CATextLayer?;
    var endDayLayer:CATextLayer?;
    var endTimeLayer:CATextLayer?;
    
    var customTimeLayer:DDropDurationTextLayer?;
    var durationLayer:DDropDurationTextLayer?;
    
    //MARK:- add layers
    func addTextLayers() {
        self.beginDayLayer = self.textLayer(UIColor.lightGray, font: self.timeModel.dayFont!)
        self.beginTimeLayer = self.textLayer(UIColor.gray, font: self.timeModel.timeFont!);
        self.endDayLayer = self.textLayer(UIColor.lightGray, font: self.timeModel.dayFont!);
        self.endTimeLayer = self.textLayer(UIColor.gray, font: self.timeModel.timeFont!);
        self.durationLayer = self.durationLayer(UIColor.black, font: self.timeModel.dayFont!);
        self.customTimeLayer = self.durationLayer(UIColor.black, font: self.timeModel.timeFont!);
        
        self.layer.addSublayer(self.beginDayLayer!);
        self.layer.addSublayer(self.beginTimeLayer!);
        self.layer.addSublayer(self.endDayLayer!);
        self.layer.addSublayer(self.endTimeLayer!);
        self.layer.addSublayer(self.durationLayer!);
        self.layer.addSublayer(self.customTimeLayer!);
    }
    
    func textLayer(_ color:UIColor, font:UIFont) -> CATextLayer{
        let layer:CATextLayer = CATextLayer();
        layer.frame = CGRect(x: 0, y: 0, width: 0, height: 20);
        layer.contentsScale = UIScreen.main.scale;
        layer.truncationMode = kCATruncationEnd;
        layer.contentsGravity = kCAGravityCenter;
        layer.alignmentMode = kCAAlignmentCenter;
        layer.foregroundColor = color.cgColor;
        let fontRef = CGFont.init(font.fontName as CFString);
        layer.font = fontRef;
        layer.fontSize = font.pointSize;
        return layer;
    }
    func durationLayer(_ color:UIColor, font:UIFont) -> DDropDurationTextLayer {
        let layer = DDropDurationTextLayer();
        layer.contentsScale = UIScreen.main.scale;
        layer.truncationMode = kCATruncationEnd;
        layer.contentsGravity = kCAGravityCenter;
        layer.alignmentMode = kCAAlignmentCenter;
        layer.foregroundColor = color.cgColor;
        let fontRef = CGFont.init(font.fontName as CFString);
        layer.font = fontRef;
        layer.fontSize = font.pointSize;
        return layer;
    }
    
    lazy var thumbLayer:CALayer = {
        let layer = CALayer();
        layer.frame = CGRect(x: 0, y: 0, width: 20, height: 20);
        layer.backgroundColor = UIColor(white: 1, alpha: 1).cgColor;
        layer.cornerRadius = 10;
        layer.masksToBounds = true;
        layer.shadowColor = UIColor.gray.cgColor;
        layer.shadowOffset = CGSize(width: 0, height: 2);
        layer.shadowOpacity = 0.5;
        layer.opacity = 0.5;
        return layer;
    }();
    lazy var trackLayer:CALayer = {
        let layer = CALayer();
        layer.backgroundColor = UIColor(white: 0.9, alpha: 1).cgColor;
        return layer;
    }();
    lazy var progressLayer:CALayer = {
        let layer = CALayer();
        layer.backgroundColor = UIColor.h_blue().cgColor;
        return layer;
    }();
    
    lazy var maskLayer:DDropTimeMarkLayer = {
        var layer:DDropTimeMarkLayer = DDropTimeMarkLayer();
        return layer;
    }();
    
    //MARK:- pan
    func addGestures() {
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(gestureRecognized(_:)));
        pan.delegate = self;
        let tap = UITapGestureRecognizer(target: self, action: #selector(gestureRecognized(_:)));
        tap.delegate = self;
        self.isUserInteractionEnabled = true;
        self.addGestureRecognizer(pan);
        self.addGestureRecognizer(tap);
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true;
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return true;
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        let location = touch.location(in: self);
        return self.trackLayer.frame.contains(location);
    }
    
    @objc func gestureRecognized(_ gesture:UIGestureRecognizer){
        let location = gesture.location(in: self);
        let width = location.x - self.progressLayer.frame.origin.x;
        var progressFrame = self.progressLayer.frame;
        progressFrame.size.width = width;
        let trackFrame = self.trackLayer.frame;
        if progressFrame.size.width >= trackFrame.size.width {
            progressFrame.size.width = trackFrame.size.width;
        }else if progressFrame.size.width <= 0 {
            progressFrame.size.width = 0;
        }
        var frame =  self.thumbLayer.frame;
        frame.origin.x = location.x - frame.size.width / 2;
        self.thumbLayer.frame = frame;
        self.progressLayer.frame = progressFrame;
    }
}
